package com.lbmb.photodrawaimanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.BiIntFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbmb.photodrawaimanager.entity.dto.TitleEntity;
import com.lbmb.photodrawaimanager.entity.req.TitleReq;
import com.lbmb.photodrawaimanager.entity.resp.TitleResp;
import com.lbmb.photodrawaimanager.mapper.TitleMapper;
import com.lbmb.photodrawaimanager.service.TitleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/27:01 下午
 */
@Service
public class TitleServiceImpl extends ServiceImpl<TitleMapper,TitleEntity> implements TitleService {

    @Autowired
    private TitleMapper titleMapper;

    @Override
    public List<TitleResp> queryAllTitle(TitleReq req){

        LambdaQueryWrapper<TitleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TitleEntity::getDeleted,0);
        queryWrapper.orderByAsc(TitleEntity::getSort);

        if (req.getIsBanner()!=null){
            queryWrapper.eq(TitleEntity::getIsBanner,req.getIsBanner());
        }else {
            queryWrapper.eq(TitleEntity::getIsBanner,0);
        }
        return titleMapper.selectList(queryWrapper).stream().map(a->{
            TitleResp titleResp = new TitleResp();
            BeanUtils.copyProperties(a,titleResp);
            return titleResp;

        }).collect(Collectors.toList());

    }

    @Override
    public Boolean saveTitle(TitleReq req) {
        TitleEntity titleEntity = new TitleEntity();
        BeanUtils.copyProperties(req,titleEntity);
        return titleMapper.insert(titleEntity)>0;
    }

    @Override
    public Boolean upStatus(Long id) {
        TitleEntity titleEntity = new TitleEntity();
        TitleEntity dbEntity = titleMapper.selectById(id);
        Integer status = dbEntity.getStatus();

        if (status==1){
            dbEntity.setStatus(0);
        }else {
            dbEntity.setStatus(1);
        }
        return titleMapper.updateById(dbEntity)>1;
    }
}
